package cn.school.student.service.impl;

import cn.school.common.utils.SysUserUtils;
import cn.school.student.dao.ExamInfoDao;
import cn.school.student.dao.ExamResultInfoDao;
import cn.school.student.dao.StudentInfoDao;
import cn.school.student.entity.StudentInfo;
import cn.school.student.service.ExamResultInfoService;
import cn.school.student.vo.ExamInfoVO;
import cn.school.student.vo.ExamResultInfoVO;
import cn.school.sys.entity.SysUser;
import cn.school.sys.entity.SysUserRole;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class ExamResultInfoServiceImpl extends ServiceImpl<ExamResultInfoDao, ExamResultInfoVO> implements ExamResultInfoService {

    @Resource
    private ExamInfoDao examInfoDao;

    @Resource
    private StudentInfoDao studentInfoDao;
    @Resource
    private ExamResultInfoDao examResultInfoDao;

    @Override
    public IPage<ExamResultInfoVO> findExamResultPageList(ExamResultInfoVO vo) {
        String username = SysUserUtils.getSysUser().getUsername();
        List<SysUserRole> roleInfoList = SysUserUtils.getSysUser().getRoleInfoList();
        List<String> roleNames = roleInfoList.stream().map(SysUserRole::getRoleName).collect(Collectors.toList());
        if (roleNames.contains("超级管理员")) {
            username = "";
        }
        vo.setClassTeacherId(username);
        
        int count =  examResultInfoDao.findExamResultPageListCount(vo);
        int startNum=(vo.getPageNum()-1)*vo.getPageSize();
        vo.setPageNum(startNum);
        List<ExamResultInfoVO> examInfoList =  examResultInfoDao.findExamResultPageList(vo);
        IPage<ExamResultInfoVO> page = new Page<>();
        page.setTotal(count);
        page.setRecords(examInfoList);
        return page;
    }

    @Override
    public int addOrUpdateExamResult(ExamResultInfoVO vo) {
        String username = SysUserUtils.getSysUser().getUsername();
        int num = 0;
        if (StringUtils.isEmpty(vo.getId())){
            //查看考试是否有成绩录入
            LambdaQueryWrapper<ExamResultInfoVO> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ExamResultInfoVO::getExamId,vo.getExamId());
            int count = this.baseMapper.selectCount(wrapper);
            if(count>0) return -1;
            List<Map<String,Object>> examResultMap =  vo.getExamStudents();
            ExamResultInfoVO saveVo = null;
            for (Map<String,Object> map : examResultMap){
                saveVo = new ExamResultInfoVO();
                saveVo.setExamId(vo.getExamId());
                saveVo.setScore(map.get("score")+"");
                saveVo.setIsPass(map.get("isPass")+"");
                saveVo.setStudentClassId(map.get("studentClassId")+"");
                saveVo.setStudentCode(map.get("studentCode")+"");
                saveVo.setStudentName(map.get("studentName")+"");
                saveVo.setStudentClassName(map.get("studentClassName")+"");
                saveVo.setCreateBy(username);
                saveVo.setCreateTime(new Date());
                this.baseMapper.insert(saveVo);
                num++;
            }
        }else{
            //删除原有结果，再做新增
            LambdaQueryWrapper<ExamResultInfoVO> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ExamResultInfoVO::getExamId,vo.getExamId());
            this.baseMapper.delete(wrapper);
            List<Map<String,Object>> examResultMap =  vo.getExamStudents();
            ExamResultInfoVO saveVo = null;
            for (Map<String,Object> map : examResultMap){
                saveVo = new ExamResultInfoVO();
                saveVo.setExamId(vo.getExamId());
                saveVo.setScore(map.get("score")+"");
                saveVo.setIsPass(map.get("isPass")+"");
                saveVo.setStudentClassId(map.get("studentClassId")+"");
                saveVo.setStudentCode(map.get("studentCode")+"");
                saveVo.setStudentName(map.get("studentName")+"");
                saveVo.setStudentClassName(map.get("studentClassName")+"");
                saveVo.setCreateBy(username);
                saveVo.setCreateTime(new Date());
                this.baseMapper.insert(saveVo);
                num++;
            }
        }
        return num;
    }

    @Override
    public int deleteResultById(ExamResultInfoVO vo) {
        return this.baseMapper.deleteById(vo.getId());
    }

    @Override
    public int deleteBatchExamResult(List<ExamResultInfoVO> list) {
        int num = 0;
        for (ExamResultInfoVO vo:list){
            LambdaQueryWrapper<ExamResultInfoVO> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ExamResultInfoVO::getExamId,vo.getId());
            this.baseMapper.delete(wrapper);
            num++;
        }
        return num;
    }

    @Override
    public int addBatchExamResult(List<ExamResultInfoVO> list) {
        int num = 0;
        String username = SysUserUtils.getSysUser().getUsername();
        for (ExamResultInfoVO vo:list){
            vo.setCreateBy(username);
            vo.setCreateTime(new Date());
            this.baseMapper.insert(vo);
            num++;
        }
        if(num==list.size()) return 1;
        return 0;
    }

    @Override
    public List<ExamInfoVO> queryExamByName(ExamInfoVO vo) {
        String username = SysUserUtils.getSysUser().getUsername();
        List<SysUserRole> roleInfoList = SysUserUtils.getSysUser().getRoleInfoList();
        List<String> roleNames = roleInfoList.stream().map(SysUserRole::getRoleName).collect(Collectors.toList());
        if (roleNames.contains("超级管理员")) {
            username = "";
        }
        vo.setClassTeacherId(username);
        return examInfoDao.findExamInfoPageList(vo);
    }

    @Override
    public List<StudentInfo> queryClassStudents(ExamInfoVO vo) {
        QueryWrapper<StudentInfo> wrapper = new QueryWrapper<>();
        wrapper.select("student_id", "name","id","class_id","class_name");
        wrapper.eq("class_id",vo.getClassId());
        List<StudentInfo> list = studentInfoDao.selectList(wrapper);
        return list;
    }

    @Override
    public List<ExamResultInfoVO> queryExamResultList(String examId) {
        QueryWrapper<ExamResultInfoVO> wrapper = new QueryWrapper<>();
        wrapper.eq("exam_id",examId);
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public void addExamResult(ExamResultInfoVO examResultInfoVO) {
        this.baseMapper.insert(examResultInfoVO);
    }
}
